package com.ymatou.iapi.shoppingcart.testcase;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcart.parameter.AddShoppingCartBean;
import com.ymatou.iapi.shoppingcart.service.AddShoppingCartCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.JsonBeanFile;

public class Ts_AddShoppingCart {
	
	private static AddShoppingCartBean addShoppingCartBean;
	private static AddShoppingCartCall addShoppingCartCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");
	private static int buyerUserId = 20226821;
	
	private static int activityStartId = Integer.parseInt(EnvSetup.getData("productActivityStartId"));
	private static String activityUser = EnvSetup.getData("AdminLoginUser");
	private static String activityPswd = EnvSetup.getData("AdminLoginPswd");
	
	private static String fileName = JsonBeanFile.CreateLiveForZyTpl;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加购物");
	}

	@Before
	public void caseUp() throws Exception {
		addShoppingCartBean = new AddShoppingCartBean();
		addShoppingCartCall = new AddShoppingCartCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addShoppingCartCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_AddShoppingCart_001() {
		 Logger.start(true,"验证添加现货至购物车是否成功");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 Map<String,Object> map = productDetailModelWapper.getProductId(productId);
			 JSONArray catalogList1 = new JSONArray(map.get("CatalogList").toString());
			 String catalogId = new JSONObject(catalogList1.get(0).toString()).getString("CatalogId");
			 
			 addShoppingCartBean.setCatalogId(catalogId);
			 addShoppingCartBean.setUserId(buyerUserId);
			 addShoppingCartBean.setPurchaseNum(1);
			 addShoppingCartBean.setSource(0);
			 addShoppingCartBean.setClient(0);
			 
			 addShoppingCartCall.setData(addShoppingCartBean);
			 addShoppingCartCall.callService();
			 
			 ShoppingCartMongoWapper shoppingCartMongoWapper = new ShoppingCartMongoWapper();
			 String productid = shoppingCartMongoWapper.
					 getOneShoppingCartInfoForMap("{\"catalogid\":\"" + catalogId + "\"}").get("productid").toString();
						
			 
			 Logger.comment("addShoppingCartCall send:" + addShoppingCartCall.getEntityString());
			 Logger.comment("addShoppingCartCall response:" + addShoppingCartCall.getReturnData());
			 
			 Logger.verifyEquals(true, addShoppingCartCall.getResult().getBoolean("success"), "验证添加现货至购物车是否成功");	
			 Logger.verifyEquals(productid, productId, "验证添加的商品是否落库");
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_AddShoppingCart_002() {
		 Logger.start(true,"验证添加直播商品至购物车是否成功");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId, fileName).getJSONObject("Data").getInt("ActivityId");
			 SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			 
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 Map<String,Object> map = productDetailModelWapper.getProductId(productId);
			 JSONArray catalogList1 = new JSONArray(map.get("CatalogList").toString());
			 String catalogId = new JSONObject(catalogList1.get(0).toString()).getString("CatalogId");
			 
			 addShoppingCartBean.setCatalogId(catalogId);
			 addShoppingCartBean.setUserId(buyerUserId);
			 addShoppingCartBean.setPurchaseNum(1);
			 addShoppingCartBean.setSource(0);
			 addShoppingCartBean.setClient(0);
			 
			 addShoppingCartCall.setData(addShoppingCartBean);
			 addShoppingCartCall.callService();
			 
			 ShoppingCartMongoWapper shoppingCartMongoWapper = new ShoppingCartMongoWapper();
			 String productid = shoppingCartMongoWapper.
					 getOneShoppingCartInfoForMap("{\"catalogid\":\"" + catalogId + "\"}").get("productid").toString();
						
			 
			 Logger.comment("addShoppingCartCall send:" + addShoppingCartCall.getEntityString());
			 Logger.comment("addShoppingCartCall response:" + addShoppingCartCall.getReturnData());
			 
			 Logger.verifyEquals(true, addShoppingCartCall.getResult().getBoolean("success"), "验证添加直播商品至购物车是否成功");	
			 Logger.verifyEquals(productid, productId, "验证添加的商品是否落库");
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_AddShoppingCart_003() {
		 Logger.start(true,"验证添加活动商品至购物车是否成功");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 SellerProductCallService.approveActivity(sellerId, activityUser, activityPswd, String.valueOf(activityStartId), productId);
			 
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 Map<String,Object> map = productDetailModelWapper.getProductId(productId);
			 JSONArray catalogList1 = new JSONArray(map.get("CatalogList").toString());
			 String catalogId = new JSONObject(catalogList1.get(0).toString()).getString("CatalogId");
			 
			 addShoppingCartBean.setCatalogId(catalogId);
			 addShoppingCartBean.setUserId(buyerUserId);
			 addShoppingCartBean.setPurchaseNum(1);
			 addShoppingCartBean.setSource(0);
			 addShoppingCartBean.setClient(0);
			 
			 addShoppingCartCall.setData(addShoppingCartBean);
			 addShoppingCartCall.callService();
			 
			 ShoppingCartMongoWapper shoppingCartMongoWapper = new ShoppingCartMongoWapper();
			 String productid = shoppingCartMongoWapper.
					 getOneShoppingCartInfoForMap("{\"catalogid\":\"" + catalogId + "\"}").get("productid").toString();
						
			 Logger.comment("addShoppingCartCall send:" + addShoppingCartCall.getEntityString());
			 Logger.comment("addShoppingCartCall response:" + addShoppingCartCall.getReturnData());
			 
			 Logger.verifyEquals(true, addShoppingCartCall.getResult().getBoolean("success"), "验证添加活动商品至购物车是否成功");	
			 Logger.verifyEquals(productid, productId, "验证添加的商品是否落库");
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_AddShoppingCart_004() {
		 Logger.start(true,"验证添加下架的商品至购物车是否失败");
		 try {
			 String productId = SellerProductCallService.addNewProductNotOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 Map<String,Object> map = productDetailModelWapper.getProductId(productId);
			 JSONArray catalogList1 = new JSONArray(map.get("CatalogList").toString());
			 String catalogId = new JSONObject(catalogList1.get(0).toString()).getString("CatalogId");
			 
			 addShoppingCartBean.setCatalogId(catalogId);
			 addShoppingCartBean.setUserId(buyerUserId);
			 addShoppingCartBean.setPurchaseNum(1);
			 addShoppingCartBean.setSource(0);
			 addShoppingCartBean.setClient(0);
			 
			 addShoppingCartCall.setData(addShoppingCartBean);
			 addShoppingCartCall.callService();
			 
			 Logger.comment("addShoppingCartCall send:" + addShoppingCartCall.getEntityString());
			 Logger.comment("addShoppingCartCall response:" + addShoppingCartCall.getReturnData());
			 
			 Logger.verifyEquals(false, addShoppingCartCall.getResult().getBoolean("success"), "验证添加下架的商品至购物车是否失败");	
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
}
